﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TB_WS.ASMX
{
    /// <summary>
    /// HongBao_Logic 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class HongBao_Logic : System.Web.Services.WebService
    {

       [WebMethod(Description ="获取二维码QR_scene是否存在")]
       public int GET_QR_SCENE_COUNT(string QR_scene_id)
        {
            return Class.HongBao_Logic.get_QR_scene_count(QR_scene_id);
        }

        [WebMethod(Description ="根据订单号获取红包列表")]
        public DataSet GET_HB_LIST_USE_TID(string Tid)
        {
            return Class.HongBao_Logic.get_QR_scene_info_use_Tid(Tid);
        }

        [WebMethod(Description ="根据QR_Scene_id获取红包详细信息")]
        public DataSet GET_HB_Info(string QR_Scene_ID)
        {
            return Class.HongBao_Logic.get_QR_scene_info(QR_Scene_ID);
        }


        [WebMethod(Description = "根据QR_URL获取红包详细信息")]
        public DataSet GET_HB_Info_Use_QR_URL(string QR_URL)
        {
            return Class.HongBao_Logic.get_QR_scene_info_URL(QR_URL);
        }

        [WebMethod(Description ="获取二维码详情")]
        public DataSet GET_QR_INFO(string QR_scene_id)
        {
            return Class.HongBao_Logic.get_QR_scene_info(QR_scene_id);
        }

        
        [WebMethod(Description = "修改QR红包的金额")]
        public int CHANGE_HONGBAO_MONEY(string QR_scene_id, int Money)
        {
            return Class.HongBao_Logic.update_Money(QR_scene_id, Money);
        }

        [WebMethod(Description = "检测QR_Scene是否支持返现")]
        public int Check_HongBao(string QR_scene_id)
        {
            DataSet HB_DST = Class.HongBao_Logic.get_QR_scene_info(QR_scene_id);
            string HB_Type = HB_DST.Tables[0].Rows[0][8].ToString();

            DataSet HB_Log_DST = Class.HB_Log.Get_HB_Log_QR_Scene_id(QR_scene_id);

            if(HB_Log_DST.Tables[0].Rows.Count!=0)
            {
                return -1;
            }
             else if (HB_Type == "Wait_Collect")
            {
                return 1;
            }
            else if (HB_Type == "UN_Collect")
            {
                return 1;
            }
            else if (HB_Type == "Wait_Collect_Rate_2")
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        [WebMethod(Description ="收红包")]
        public int COLLECT_HONGBAO(string QR_scene_id)
        {
            DataSet HB_DST = Class.HongBao_Logic.get_QR_scene_info(QR_scene_id);
            string HB_Type = HB_DST.Tables[0].Rows[0][8].ToString();
            if (HB_Type == "Wait_Collect")
            {
                int Collect_Count = Class.HongBao_Logic.update_Type(QR_scene_id,
                    "Collected", "红包已于：" + DateTime.Now.ToString() + " 收取");
                string[] Tids = HB_DST.Tables[0].Rows[0][4].ToString().Split(',');
                for (int i = 0; i < Tids.Length; i++)
                {
                    int Seller_ID = Convert.ToInt32(HB_DST.Tables[0].Rows[0][1].ToString());
                    DataSet Seller_Info = Class.Seller_Manage.select_Seller_Info(Seller_ID);
                    Class.Top_Logic CTL = new Class.Top_Logic(Seller_Info.Tables[0].Rows[0][1].ToString());

                    CTL.Set_Seller_Memo(Tids[i], "--自动发红包系统：红包已于" + DateTime.Now.ToString() + "收取");
                }
                return Collect_Count;
            }
            else if (HB_Type == "UN_Collect")
            {
                int Collect_Count = Class.HongBao_Logic.update_Type(QR_scene_id,
                    "Collected", "红包已于：" + DateTime.Now.ToString() + " 收取");
                return Collect_Count;
            }
            else if (HB_Type == "Wait_Collect_Rate_2")
            {
                //有需要回评订单
                int Collect_Count = Class.HongBao_Logic.update_Type(QR_scene_id,
                    "Collected", "红包已于：" + DateTime.Now.ToString() + " 收取");
                return Collect_Count;
            }
            else
            {
                return -1;//红包已被领取或状态异常
            }
        }

        public int Check_HB_Type(string QR_scene_id)
        {
            DataSet HB_DST = Class.HongBao_Logic.get_QR_scene_info(QR_scene_id);
            string HB_Type = HB_DST.Tables[0].Rows[0][8].ToString();
            if (HB_Type == "Wait_Collect")
            {
                return 0;
            }
            else if (HB_Type == "UN_Collect")
            {
                return 0;
            }
            else if (HB_Type == "Wait_Collect_Rate_2")
            {
                return 0;
            }
            else
            {
                return -1;//红包已被领取或状态异常
            }
        }



        [WebMethod(Description ="作废红包")]
        public int END_COLLECT(string QR_scene_id,string contect)
        {
            return Class.HongBao_Logic.update_Type(QR_scene_id, "End",
                contect);
        }

        [WebMethod(Description ="打印NONE红包状态变更")]
        public int PRINT_NONE_HB(string QR_scene_id, string contect)
        {
            return Class.HongBao_Logic.update_Type(QR_scene_id, "Wait_Mate",
                contect);
        }

        [WebMethod(Description ="恢复红包状态")]
       　public int Refresh_HB(string QR_Scene_id,string Type)
        {
            return Class.HongBao_Logic.update_Type(QR_Scene_id, Type,
                "");
        }


        [WebMethod(Description ="获取红包列表信息")]
        public DataSet GET_HongBao_List_Where(int Seller_ID,string Where_Str)
        {
            return Class.HB_Log.get_HongBao_Where(Seller_ID, Where_Str);
        }

        [WebMethod(Description ="获取红包领取信息列表")]
        public DataSet GET_HongBao_Get_Where(int Seller_ID, string Where_Str)
        {
            return Class.HB_Log.get_Get_HB_Where(Seller_ID, Where_Str);
        }

        [WebMethod(Description ="新增一个红包--服务器端")]
        public string INSERT_HB_SERVER(int Seller_ID, string WX_User,
            string Create_From, string Tids, double Money, string Type)
        {
            return Class.HongBao_Logic.insert_HB_Server(Seller_ID, WX_User,
                Create_From, Tids, Money, Type);
        }

        [WebMethod(Description ="获取红包发放详情")]
        public DataSet GET_HB_LOG_QR_Scene_ID(string QR_Scene_ID)
        {
            return Class.HB_Log.Get_HB_Log_QR_Scene_id(QR_Scene_ID);
        }

        [WebMethod(Description ="匹配红包")]
        public string MATE_HB(string QR_URL, string Tids, int Money, string Type)
        {
            if(Class.HongBao_Logic.get_QR_scene_info_URL(QR_URL).Tables[0].Rows[0][8].ToString()=="Wait_Mate")
            {
                //可以继续
                Class.HongBao_Logic.Mate_HB(QR_URL, Tids, Money, Type);
                return "1#匹配成功";
            }
            else
            {
                //状态不正确
                return "0#当前二维码状态为：" + Class.HongBao_Logic.get_QR_scene_info_URL(QR_URL).Tables[0].Rows[0][8].ToString() + " 请检查状态";
            }
        }

        [WebMethod(Description ="获取未打印红包数量")]
        public int GET_WAIT_PRINT_COUNT(int Seller_ID)
        {
            return Class.HongBao_Logic.get_HB_None_Count(Seller_ID);
        }

        [WebMethod(Description ="获取未打印红包列表")]
        public DataSet GET_WP_HB_LIST(int Seller_ID, int Max)
        {
            return Class.HongBao_Logic.GET_HB_NONE_LIST(Seller_ID, Max);
        }

       
    }
}
